/**
 * Copyright (c) 2010 CWI Amsterdam and others. All rights reserved.
 * This program and the accompanying materials are made available
 * under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    CWI Amsterdam - initial API and implementation
 */
package nl.cwi.arne.mcrl2.toolchains;

import java.io.File;

import nl.cwi.arne.mcrl2.Specification;
import nl.cwi.arne.mcrl2.util.MCRL2Commands;
import nl.cwi.arne.mcrl2.util.MCRL2FileUtil;
import nl.cwi.arne.mcrl2.util.MCRL2Options;
import nl.cwi.arne.mcrl2.util.ToolChain;

/**
 * @author Christian Krause
 * @generated NOT
 */
public class GenerateLPSToolChain extends ToolChain {
	
	// Serial ID:
	private static final long serialVersionUID = 1L;
	
	// Involved files:
	private File mcrl2File, lpsFile1, lpsFile2;
	
	// Base of all file names:
	private String baseFileName;
	
	/**
	 * Constructor.
	 * @param specification mCRL2 specification.
	 */
	public GenerateLPSToolChain(Specification specification, MCRL2Options options) {
		
		super("Generating linear process specification...");
		setWaitForLast(true);
		
		mcrl2File = MCRL2FileUtil.saveSpecification(specification);
		baseFileName = mcrl2File.getAbsolutePath();
		baseFileName = baseFileName.substring(0, baseFileName.length()-6);
		
		lpsFile1 = new File(baseFileName + "1.lps");
		lpsFile2 = options.getLpssumelm() ? new File(baseFileName + "2.lps") : lpsFile1;
		
		add(MCRL2Commands.mcrl22lps(mcrl2File, lpsFile1));
		if (options.getLpssumelm()) {
			add(MCRL2Commands.lpssumelm(lpsFile1, lpsFile2));
		}
		
	}
	
	/**
	 * Protected constructor. Can be used by subclasses that 
	 * generate the LPS separately (or that have it cached).
	 * This constructor doesn't add any command to the tool chain.
	 * @param lpsFile LPS file to be used.
	 */
	protected GenerateLPSToolChain(File lpsFile) {
		super("Generating linear process specification...");
		setWaitForLast(true);
		lpsFile2 = lpsFile;
		baseFileName = lpsFile.getAbsolutePath();
		baseFileName = baseFileName.substring(0, baseFileName.length()-4);
	}

	protected GenerateLPSToolChain(String name) {
		super(name);
	}

	/*
	 * (non-Javadoc)
	 * @see nl.cwi.arne.mcrl2.util.ToolChain#doCleanup()
	 */
	@Override
	protected void doCleanup() {
		if (mcrl2File!=null) {
			mcrl2File.delete();
		}
		if (lpsFile1!=null && lpsFile2!=null && !lpsFile1.equals(lpsFile2)) {
			lpsFile1.delete();
		}
	}
	
	protected String getBaseFileName() {
		return baseFileName;
	}
	
	public File getLPSFile() {
		return lpsFile2;
	}
	
}
